package com.bto.solar.work.service;

import com.bto.solar.work.common.BizException;
import com.bto.solar.work.dao.IStoImportBaseService;
import com.bto.solar.work.dao.IStoImportDeviceService;
import com.bto.solar.work.entity.StoImportBase;
import com.bto.solar.work.entity.StoImportDevice;
import com.bto.solar.work.utils.NullUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 王小波
 * @description:  仓库收货
 * @date 2023/9/8 17:22
 */

@Slf4j
@Service
public class BtoStoImportService {
    @Autowired
    private IStoImportBaseService stoImportBaseService;
    @Autowired
    private IStoImportDeviceService stoImportDeviceService;


    /**
    * 验证扫码设备是否有效
    */
    public void checkProjectScannedDeviceIsValid(String orderId , String deviceType , List<String> deviceSnList){
        StoImportBase importRecord = stoImportBaseService.lambdaQuery().eq(StoImportBase::getStoId, orderId).one();
        if(NullUtils.isNull(importRecord)){
            throw new BizException("该项目，设备还未进行现场收货，无法扫码");
        }

        StoImportDevice importDevice = stoImportDeviceService.lambdaQuery()
                .eq(StoImportDevice::getImpId, importRecord.getImpId())
                .eq(StoImportDevice::getDeviceType, deviceType).one();

        for (String deviceSn : deviceSnList){
            if(!importDevice.getDeviceIds().contains(deviceSn)){
                throw new BizException("扫码验证： 订单{} 设备类型 {} 编号 {} 不在出货设备表里 {}"  ,orderId , deviceType , deviceSn , deviceSnList);
            }
        }
    }


}
